package blog.service.impl;

import blog.dao.FeedbackDao;
import blog.pojo.Feedback;
import blog.service.FeedbackService;
import blog.util.Timestamp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FeedbackServiceImpl implements FeedbackService {


    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    public Page<Feedback> getPage(Page page, Feedback feedback, String keywords) throws Exception {
        if (page == null) return null;
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>(feedback)
                .ne("isuse", 2);
        if (keywords != null) {
            queryWrapper.and(Wrapper -> Wrapper.like("title", keywords).or().like("content", keywords).or().like("sender", keywords));
        }
        return feedbackDao.selectPage(page, queryWrapper);
    }

    @Override
    public Feedback getInfo(Integer id) throws Exception {
        return feedbackDao.selectById(id);
    }

    @Override
    public Boolean update(Feedback feedback) throws Exception {
        feedback.setGmtUpdate(Timestamp.gen());
        return feedbackDao.updateById(feedback) > 0;
    }

    @Override
    public Boolean insert(Feedback feedback) throws Exception {
        feedback.setGmtCreate(Timestamp.gen())
                .setGmtUpdate(Timestamp.gen())
                .setIsuse(1);
        return feedbackDao.insert(feedback) > 0;
    }
}
